package com.car.advertising.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.car.advertising.R;
import com.car.advertising.activity.adapter.AutomobileBrandAdapter;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.bean.AutomobileBrandBean;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.BaseRecylerViewToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class AutomobileBrandActivity extends BaseRecylerViewToolBarActivity {

    private List<AutomobileBrandBean> AutomobileBrandList = new ArrayList<>();
    private AutomobileBrandAdapter automobileBrandAdapter;

    @Override
    protected void initToolBar() {
        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("车辆品牌");
        setToolBarTitleColor(R.color.black_bg);

        setPullDownEnabled(false);
        setPullUpEnabled(false);
    }

    @Override
    protected void init() {
        getAutomobileBrandData();
        setAutomobileBrandList();
    }

    private void setAutomobileBrandList() {
        if (automobileBrandAdapter == null) {
            automobileBrandAdapter = new AutomobileBrandAdapter(this, AutomobileBrandList);
            initRecyclerView(automobileBrandAdapter);
            automobileBrandAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(ViewGroup parent, View view, int position) {
                    AutomobileBrandBean brandBean = AutomobileBrandList.get(position);
                    Intent intent = getIntent();
                    intent.putExtra("brandBean", brandBean);
                    setResult(1, intent);
                    finish();
                }
            });
        } else {
            automobileBrandAdapter.setRefreshData(AutomobileBrandList);
        }
    }

    private void getAutomobileBrandData() {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_040, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(String request) {
            }

            @Override
            public void onResponse(String resp) {
                LogUtil.i(resp);
                try {
                    JSONArray array = new JSONArray(resp);
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        String id = jsonObject.getString("id");
                        String brandCode = jsonObject.getString("brandCode");
                        String brandName = jsonObject.getString("brandName");
                        String carModel = jsonObject.getString("carModel");
                        AutomobileBrandList.add(new AutomobileBrandBean(id, brandCode, brandName, carModel));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                setAutomobileBrandList();
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }


    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }

    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }
}
